<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>扩展</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="reflection.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="class.reflection.html">Reflection</a></div>
 <div class="up"><a href="book.reflection.html">反射</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="reflection.extending" class="chapter">
 <h1>扩展</h1>

 <p class="para">
  如果你想创建内建类的专门版本（比如说，在创建并导出高亮 HTML 时，以易于访问的成员变量来取代方法或使用实用的方法），
  你可以继续并扩展它们。
 </p>
 <div class="example" id="example-5082">
  <p><strong>Example #1 扩展内置的类</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;My&nbsp;Reflection_Method&nbsp;class<br />&nbsp;*/<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">My_Reflection_Method&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">ReflectionMethod<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$visibility&nbsp;</span><span style="color: #007700">=&nbsp;array();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$o</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$o</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">visibility&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Reflection</span><span style="color: #007700">::</span><span style="color: #0000BB">getModifierNames</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getModifiers</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;Demo&nbsp;class&nbsp;#1<br />&nbsp;*<br />&nbsp;*/<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">T&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;</span><span style="color: #0000BB">x</span><span style="color: #007700">()&nbsp;{}<br />}<br /><br /></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;Demo&nbsp;class&nbsp;#2<br />&nbsp;*<br />&nbsp;*/<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">U&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">T&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">x</span><span style="color: #007700">()&nbsp;{}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;输出信息<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">My_Reflection_Method</span><span style="color: #007700">(</span><span style="color: #DD0000">'U'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'x'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  <div class="example-contents"><p>以上例程的输出类似于：</p></div>
  <div class="example-contents screen">
<div class="cdata"><pre>
object(My_Reflection_Method)#1 (3) {
  [&quot;visibility&quot;]=&gt;
  array(1) {
    [0]=&gt;
    string(6) &quot;public&quot;
  }
  [&quot;name&quot;]=&gt;
  string(1) &quot;x&quot;
  [&quot;class&quot;]=&gt;
  string(1) &quot;U&quot;
}
</pre></div>
  </div>
 </div>
 <div class="caution"><strong class="caution">Caution</strong>
  <p class="para">
   如果你重写了构造函数，记住在写任何插入的代码之前要先调用父类的构造函数。
   不这么做将会导致以下的结果：
   <em>Fatal error: Internal error: Failed to retrieve the reflection object</em>
  </p>
 </div>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="reflection.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="class.reflection.html">Reflection</a></div>
 <div class="up"><a href="book.reflection.html">反射</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
